<template>
    <div class="sidebar">
        <el-menu
            class="sidebar-el-menu"
            :default-active="onRoutes"
            :collapse="sidebarCollapse"
            background-color="#324157"
            text-color="#bfcbd9"
            active-text-color="#20a0ff"
            unique-opened
        >
            <template v-for="item in items">
                <template v-if="item.subs">
                    <el-submenu :index="item.index" v-if="!item.disabled" :key="item.index">
                        <template slot="title">
                            <i :class="item.icon"></i>
                            <span slot="title">{{ item.title }}</span>
                        </template>
                        <template v-for="subItem in item.subs">
                            <el-submenu v-if="subItem.subs && !subItem.disabled" :index="subItem.index" :key="subItem.index">
                                <template slot="title">{{ subItem.title }}</template>
                                <template v-for="(threeItem, i) in subItem.subs">
                                    <el-menu-item
                                        :key="i"
                                        @click="ItemClick(threeItem)"
                                        v-if="!threeItem.disabled"
                                        :index="threeItem.index"
                                        >{{ threeItem.title }}</el-menu-item
                                    >
                                </template>
                            </el-submenu>
                            <el-menu-item
                                v-else-if="!subItem.disabled"
                                @click="ItemClick(subItem)"
                                :index="subItem.index"
                                :key="subItem.index"
                                >{{ subItem.title }}</el-menu-item
                            >
                        </template>
                    </el-submenu>
                </template>
                <template v-else>
                    <el-menu-item :index="item.index" v-if="!item.disabled" @click="ItemClick(item)" :key="item.index">
                        <i :class="item.icon"></i>
                        <span slot="title">{{ item.title }}</span>
                    </el-menu-item>
                </template>
            </template>
        </el-menu>
    </div>
</template>

<script>
import bus from '../common/bus';
import { mapState, mapMutations } from 'vuex';
export default {
    data() {
        return {
            items: [
                {
                    icon: 'el-icon-lx-home',
                    index: 'dashboard',
                    title: '系统首页'
                },
                {
                    icon: 'el-icon-chat-line-square',
                    index: 'newsTable',
                    title: '新闻发布'
                },
                {
                    icon: 'el-icon-box',
                    index: 'packageInFlow',
                    title: '入库流程',
                    subs: [
                        {
                            index: 'packageClaimTable',
                            title: '包裹预报'
                        },
                        {
                            index: 'packageInTable',
                            title: '包裹录单'
                        },
                        {
                            index: 'repositoryInTable',
                            title: '包裹上架'
                        }
                    ]
                },
                {
                    icon: 'el-icon-truck',
                    index: 'packageOutFlow',
                    title: '出库流程',
                    subs: [
                        {
                            index: 'packageApplyOutTable',
                            title: '申请出货'
                        },
                        {
                            index: 'packageDeliverTable',
                            title: '订单出库'
                        }
                    ]
                },

                {
                    icon: 'el-icon-takeaway-box',
                    index: 'repositoryManage',
                    title: '库存',
                    subs: [
                        {
                            index: 'repositoryTable',
                            title: '库存管理'
                        }
                    ]
                },
                {
                    icon: 'el-icon-lx-cascades',
                    index: 'feeManage',
                    title: '财务管理',
                    subs: [
                        {
                            index: 'income',
                            title: '收入'
                        },
                        {
                            index: 'runFee',
                            title: '运营费用'
                        },
                        {
                            index: 'agentFee',
                            title: '出货成本管理'
                        },
                        {
                            index: 'packageAccountTable',
                            title: '订单清账'
                        },
                        {
                            index: 'profitTable',
                            title: '收支核算'
                        },

                        {
                            index: 'orderprofitTable',
                            title: '订单核算'
                        }
                    ]
                },
                {
                    icon: 'el-icon-set-up',
                    index: 'userManager',
                    title: '系统设置',
                    subs: [
                        {
                            index: 'sysuser',
                            title: '系统用户管理'
                        },
                        {
                            index: 'roleManage',
                            title: '系统角色管理'
                        },
                        {
                            index: 'appuser',
                            title: 'APP用户管理'
                        },
                        {
                            index: 'userAccountCharge',
                            title: 'APP用户充值'
                        }
                    ]
                },
                {
                    icon: 'el-icon-lx-copy',
                    index: 'restfulModelTable',
                    title: '基础设置',
                    subs: [
                        {
                            index: 'userChargeQrCodeImage',
                            query: { title: '小程序支付二维码' },
                            title: '小程序支付二维码'
                        },
                        {
                            index: 'restfulModelTable?modelKey=SystemSetting',
                            query: { title: '系统设置' },
                            title: '系统设置'
                        },
                        {
                            index: 'restfulModelTable?modelKey=Currency',
                            query: { title: '货币管理' },
                            title: '货币管理'
                        },
                        {
                            index: 'currencyExchangeRate',
                            title: '汇率管理'
                        },
                        {
                            index: 'nationTable',
                            title: '国家管理'
                        },
                        {
                            index: 'restfulModelTable?modelKey=Repository',
                            query: { title: '仓库管理' },
                            title: '仓库管理'
                        },
                        {
                            index: 'repositoryRegionManage',
                            query: { title: '仓库区域管理' },
                            title: '仓库区域管理'
                        },
                        {
                            index: 'goodTypeManage',
                            query: { title: '商品类型管理' },
                            title: '商品类型管理'
                        },
                        {
                            index: 'channelTypeManage',
                            title: '渠道货物类型管理'
                        },
                        {
                            index: 'restfulModelTable?modelKey=Courier',
                            query: { title: '快递管理' },
                            title: '快递管理'
                        },
                        {
                            index: 'agentManageTable',
                            title: '代理管理'
                        },
                        {
                            index: 'courierCannelInTable',
                            title: '用户渠道管理'
                        }
                    ]
                }
            ]
        };
    },
    computed: {
        onRoutes() {
            return this.$route.path.replace('/', '');
        },
        ...mapState(['limitsAuthority', 'sidebarCollapse'])
    },
    created() {
        // 通过 Event Bus 进行组件间通信，来折叠侧边栏
        // bus.$on('collapse', msg => {
        //   this.collapse = msg;
        //   bus.$emit('collapse-content', msg);
        // });
        if (!this.limitsAuthority.isDefaultRole) {
            this.CheckAuthority(this.items);
        }
    },
    methods: {
        //遍历菜单项,对无权限菜单添加disabled
        CheckAuthority(list) {
            list.map((it) => {
                it.disabled = this.limitsAuthority.menuDict[it.index] != true;
                if (it.subs) {
                    this.CheckAuthority(it.subs);
                }
            });
        },
        ItemClick(item) {
            this.$router.push({ path: item.index, query: item.query });
        }
    }
};
</script>

<style scoped>
.sidebar {
    display: block;
    position: absolute;
    left: 0;
    top: 70px;
    bottom: 0;
    overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
    width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
    width: 250px;
}
.sidebar > ul {
    height: 100%;
}
</style>
